<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>算法可视化：存在重复元素</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            background-color: #f8f9fa;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background-color: #2d3748;
            color: #f8f9fa;
            border-radius: 0.5rem;
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight {
            position: relative;
        }
        .highlight::after {
            content: '';
            position: absolute;
            left: 0;
            bottom: 0;
            width: 100%;
            height: 30%;
            background-color: rgba(110, 142, 251, 0.2);
            z-index: -1;
            transform: skewY(-1deg);
        }
    </style>
</head>
<body class="min-h-screen">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl">
            <h1 class="text-4xl md:text-6xl font-bold mb-6 font-serif tracking-tight">存在<span class="highlight">重复元素</span></h1>
            <p class="text-xl md:text-2xl mb-8 max-w-2xl leading-relaxed">探索如何高效判断数组中是否存在重复元素的算法原理与实现</p>
            <div class="flex items-center">
                <span class="inline-block mr-4 text-blue-100">
                    <i class="fas fa-shield-alt text-2xl"></i>
                </span>
                <span class="text-blue-100">哈希表 | 集合 | 排序算法</span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 md:px-0 py-16">
        <!-- Problem Statement -->
        <section class="mb-20">
            <div class="flex items-center mb-6">
                <h2 class="text-3xl font-bold text-gray-800 flex items-center">
                    <span class="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center text-white mr-3">
                        1
                    </span>
                    问题描述
                </h2>
                <div class="ml-4 flex-1 h-px bg-gradient-to-r from-blue-400 to-purple-400"></div>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-8 card-hover">
                <p class="text-lg text-gray-700 leading-relaxed mb-6">
                    给定一个整数数组，判断是否存在重复元素。如果数组中任何元素出现至少两次，函数返回 <span class="font-bold text-blue-600">true</span>；如果数组中每个元素都不相同，则返回 <span class="font-bold text-blue-600">false</span>。
                </p>
                <div class="flex flex-col md:flex-row gap-6">
                    <div class="flex-1">
                        <h3 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
                            <i class="fas fa-lightbulb text-yellow-500 mr-2"></i>
                            示例
                        </h3>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <span class="inline-block bg-blue-100 text-blue-800 rounded-full px-3 py-1 text-sm mr-3">例1</span>
                                <div>
                                    <span class="font-mono">输入: [1, 2, 3, 1]</span><br>
                                    <span class="font-mono">输出: true</span>
                                </div>
                            </li>
                            <li class="flex items-start">
                                <span class="inline-block bg-blue-100 text-blue-800 rounded-full px-3 py-1 text-sm mr-3">例2</span>
                                <div>
                                    <span class="font-mono">输入: [1, 2, 3, 4]</span><br>
                                    <span class="font-mono">输出: false</span>
                                </div>
                            </li>
                        </ul>
                    </div>
                    <div class="flex-1">
                        <div class="mermaid">
                            graph TD
                                A[输入数组] --> B{是否存在重复元素?}
                                B -->|是| C[返回 true]
                                B -->|否| D[返回 false]
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Solution Approaches -->
        <section class="mb-20">
            <div class="flex items-center mb-6">
                <h2 class="text-3xl font-bold text-gray-800 flex items-center">
                    <span class="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center text-white mr-3">
                        2
                    </span>
                    解题思路
                </h2>
                <div class="ml-4 flex-1 h-px bg-gradient-to-r from-blue-400 to-purple-400"></div>
            </div>
            <div class="grid md:grid-cols-2 gap-8">
                <!-- Hash Set Method -->
                <div class="bg-white rounded-xl shadow-lg p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <span class="bg-blue-500 text-white rounded-full w-10 h-10 flex items-center justify-center mr-4">
                            <i class="fas fa-database"></i>
                        </span>
                        <h3 class="text-2xl font-bold text-gray-800">哈希表方法</h3>
                    </div>
                    <p class="text-gray-700 mb-6 leading-relaxed">
                        使用集合记录已遍历的元素，若当前元素已存在于集合中，则返回 <span class="font-bold text-blue-600">True</span>。
                    </p>
                    <div class="mb-6">
                        <div class="flex items-center text-gray-600 mb-2">
                            <i class="fas fa-clock mr-2"></i>
                            <span>时间复杂度: O(n)</span>
                        </div>
                        <div class="flex items-center text-gray-600">
                            <i class="fas fa-memory mr-2"></i>
                            <span>空间复杂度: O(n)</span>
                        </div>
                    </div>
                    <div class="mermaid">
                        graph LR
                            A[初始化空集合] --> B[遍历数组]
                            B --> C{当前元素是否在集合中?}
                            C -->|是| D[返回true]
                            C -->|否| E[将元素加入集合]
                            E --> B
                            B --> F[遍历结束]
                            F --> G[返回false]
                    </div>
                </div>

                <!-- Sorting Method -->
                <div class="bg-white rounded-xl shadow-lg p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <span class="bg-purple-500 text-white rounded-full w-10 h-10 flex items-center justify-center mr-4">
                            <i class="fas fa-sort-amount-up"></i>
                        </span>
                        <h3 class="text-2xl font-bold text-gray-800">排序方法</h3>
                    </div>
                    <p class="text-gray-700 mb-6 leading-relaxed">
                        先排序数组，然后检查相邻元素是否相同。这种方法不需要额外空间，但会修改原数组。
                    </p>
                    <div class="mb-6">
                        <div class="flex items-center text-gray-600 mb-2">
                            <i class="fas fa-clock mr-2"></i>
                            <span>时间复杂度: O(n log n)</span>
                        </div>
                        <div class="flex items-center text-gray-600">
                            <i class="fas fa-memory mr-2"></i>
                            <span>空间复杂度: O(1)</span>
                        </div>
                    </div>
                    <div class="mermaid">
                        graph LR
                            A[排序数组] --> B[初始化i=0]
                            B --> C{i < n-1?}
                            C -->|是| D{arr[i] == arr[i+1]?}
                            D -->|是| E[返回true]
                            D -->|否| F[i++]
                            F --> C
                            C -->|否| G[返回false]
                    </div>
                </div>
            </div>
        </section>

        <!-- Code Implementation -->
        <section class="mb-20">
            <div class="flex items-center mb-6">
                <h2 class="text-3xl font-bold text-gray-800 flex items-center">
                    <span class="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center text-white mr-3">
                        3
                    </span>
                    代码实现
                </h2>
                <div class="ml-4 flex-1 h-px bg-gradient-to-r from-blue-400 to-purple-400"></div>
            </div>
            <div class="bg-white rounded-xl shadow-lg overflow-hidden card-hover">
                <div class="bg-gray-800 px-6 py-3 flex items-center">
                    <div class="flex space-x-2 mr-4">
                        <span class="w-3 h-3 rounded-full bg-red-500"></span>
                        <span class="w-3 h-3 rounded-full bg-yellow-500"></span>
                        <span class="w-3 h-3 rounded-full bg-green-500"></span>
                    </div>
                    <span class="text-gray-300 font-mono">contains_duplicate.py</span>
                </div>
                <div class="p-6">
                    <div class="code-block rounded-lg p-6 mb-8">
                        <pre class="text-gray-100 overflow-x-auto">
                            <code class="font-mono">
<span class="text-purple-400">def</span> <span class="text-blue-400">containsDuplicate</span>(nums):
    <span class="text-gray-500">"""使用集合判断是否存在重复元素"""</span>
    <span class="text-purple-400">return</span> <span class="text-blue-400">len</span>(nums) != <span class="text-blue-400">len</span>(<span class="text-blue-400">set</span>(nums))
                            </code>
                        </pre>
                    </div>
                    <div class="flex items-center bg-blue-50 rounded-lg p-4">
                        <div class="text-blue-500 mr-4">
                            <i class="fas fa-info-circle text-xl"></i>
                        </div>
                        <div>
                            <h4 class="font-bold text-gray-800 mb-1">简洁性说明</h4>
                            <p class="text-gray-700">
                                这段代码利用了Python集合自动去重的特性，通过比较原数组长度和去重后集合长度的差异来判断是否存在重复元素，是最高效的实现方式之一。
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Performance Comparison -->
        <section>
            <div class="flex items-center mb-6">
                <h2 class="text-3xl font-bold text-gray-800 flex items-center">
                    <span class="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center text-white mr-3">
                        4
                    </span>
                    性能对比
                </h2>
                <div class="ml-4 flex-1 h-px bg-gradient-to-r from-blue-400 to-purple-400"></div>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-8 card-hover">
                <div class="mermaid">
                    barChart
                        title 时间复杂度对比
                        x-axis 方法
                        y-axis 时间
                        bar "哈希表" : O(n)
                        bar "排序" : O(n log n)
                </div>
                <div class="mt-8 grid md:grid-cols-2 gap-6">
                    <div class="bg-blue-50 rounded-lg p-6">
                        <h3 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
                            <i class="fas fa-bolt text-blue-500 mr-2"></i>
                            哈希表方法优势
                        </h3>
                        <ul class="space-y-3 text-gray-700">
                            <li class="flex items-start">
                                <span class="text-blue-500 mr-2">✓</span>
                                线性时间复杂度，处理大规模数据更高效
                            </li>
                            <li class="flex items-start">
                                <span class="text-blue-500 mr-2">✓</span>
                                代码简洁明了，易读性强
                            </li>
                            <li class="flex items-start">
                                <span class="text-blue-500 mr-2">✓</span>
                                不修改原始数组数据
                            </li>
                        </ul>
                    </div>
                    <div class="bg-purple-50 rounded-lg p-6">
                        <h3 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
                            <i class="fas fa-sort-amount-up text-purple-500 mr-2"></i>
                            排序方法适用场景
                        </h3>
                        <ul class="space-y-3 text-gray-700">
                            <li class="flex items-start">
                                <span class="text-purple-500 mr-2">✓</span>
                                当内存空间非常有限时
                            </li>
                            <li class="flex items-start">
                                <span class="text-purple-500 mr-2">✓</span>
                                后续还需要排序后的数组
                            </li>
                            <li class="flex items-start">
                                <span class="text-purple-500 mr-2">✓</span>
                                对于小规模数据可能更快
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>